//
//  CYTool.m
//  testNSDate
//
//  Created by baidu on 6/6/16.
//  Copyright © 2016 caoyuan. All rights reserved.
//

#import "CYTool.h"

@implementation CYTool


+ (NSDate *)dateFromDateString:(NSString *)dateStr
                    withFormat:(NSString *)dateFormat {
    if (dateFormat) {
        NSDateFormatter *format=[[NSDateFormatter alloc] init];
        [format setDateFormat:dateFormat];
        NSDate *fromdate = [format dateFromString:dateStr];
        NSTimeZone *fromzone   = [NSTimeZone systemTimeZone];
        NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
        fromdate       = [fromdate  dateByAddingTimeInterval: frominterval];
        
        return fromdate;
    }else {
        return  nil;
    }
}

+ (NSString *)stringFromDate:(NSDate *)date
                  withFormat:(NSString *)dateFormat {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    NSDate *fromdate = date;
    NSTimeZone *fromzone   = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    fromdate       = [fromdate  dateByAddingTimeInterval: -frominterval];
    
    NSString *destDateString = [dateFormatter stringFromDate:fromdate];
    
    return destDateString;
}
@end
